﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ledict.ConditionExtract.Controls;

/// <summary>
/// 自定义combox类 带下拉框及显示框显示所有勾选内容
/// </summary>
partial class ComboBoxCheckBoxTreeViewNormal : ComboBox
{
    private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
    ToolStripControlHost treeViewHost;
    ToolStripDropDown dropDown;

    StringBuilder nodesTag;
    public StringBuilder nodesName = new StringBuilder();
    public List<string> checkNodeList = new List<string>();
    public Dictionary<string, List<string>> dics = new Dictionary<string, List<string>>();

    public ComboBoxCheckBoxTreeViewNormal()
    {
        TreeView treeView = new TreeView();
        //treeView.AfterSelect += new TreeViewEventHandler(treeView_AfterSelect);
        treeView.BorderStyle = BorderStyle.None;
        treeView.CheckBoxes = true;
        treeView.AfterCheck += new TreeViewEventHandler(TreeView_AfterCheck);
        treeViewHost = new ToolStripControlHost(treeView);
        dropDown = new ToolStripDropDown();
        dropDown.Width = this.Width;

        #region 添加本级，父级及子级功能
        //Panel p = new Panel();
        //p.SuspendLayout();
        //p.Size = new System.Drawing.Size(200, 25);

        //p.Controls.AddRange(new Control[] { btn, btn1 });


        //btn.Location = new System.Drawing.Point(13, 0);
        //btn.Size = new System.Drawing.Size(80, 23);
        //btn.Text = "父级及子级";
        //btn.Click += Btn_Click;
        //btn1.Location = new System.Drawing.Point(101, 0);
        //btn1.Size = new System.Drawing.Size(75, 23);
        //btn1.Text = "只选本级";
        //btn1.Click += Btn1_Click;
        //btn1.BackColor = System.Drawing.Color.Red;

        //treeViewHost1 = new ToolStripControlHost(p); 
        #endregion
        dropDown.Items.Add(treeViewHost);
        //dropDown.Items.Add(treeViewHost1);
    }

    #region 本级，父级子级按钮事件
    //private void Btn1_Click(object sender, EventArgs e)
    //{
    //    b = false;            
    //    btn1.BackColor= System.Drawing.Color.Red;
    //    btn.BackColor = System.Drawing.SystemColors.Control;

    //}

    //private void Btn_Click(object sender, EventArgs e)
    //{
    //    b = true;
    //    btn.BackColor = System.Drawing.Color.Red;
    //    btn1.BackColor = System.Drawing.SystemColors.Control;
    //} 
    #endregion

    private void TreeView_AfterCheck(object sender, TreeViewEventArgs e)
    {
        if (e.Action != TreeViewAction.Unknown)
        {
            nodesName.Clear();
            //TreeNode node = e.Node;

            #region 未用
            //if (node.Tag == null)
            //{
            //    node.Tag = tag;//附加节点信息
            //}
            //else
            //{
            //    node.Tag = null;
            //}
            //if (b)
            //{
            //    if (node.Checked == true)
            //    {
            //        node.ExpandAll();
            //    }
            //    CheckAllChildNodes(e.Node, e.Node.Checked);
            //}
            //选中父节点
            //bool bol = true;
            //if (e.Node.Parent != null)
            //{
            //    for (int i = 0; i < e.Node.Parent.Nodes.Count; i++)
            //    {
            //        if (!e.Node.Parent.Nodes[i].Checked)
            //        {
            //            bol = false;
            //        }
            //    }
            //    e.Node.Parent.Checked = bol;

            //    if (bol)
            //    {
            //        e.Node.Parent.Tag = tag;
            //    }
            //    else
            //    {
            //        e.Node.Parent.Tag = null;
            //    }
            //} 
            #endregion

            this.Text = GetCheckNode(); ;
            //this.Text = e.Node.Text.ToString();
        }
    }

    private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
    {
        foreach (TreeNode node in treeNode.Nodes)
        {
            node.Checked = nodeChecked;
            // node.Tag = nodeChecked ? tag : null;

            if (node.Nodes.Count > 0)
            {
                this.CheckAllChildNodes(node, nodeChecked);
            }

        }
    }

    public string GetCheckNode()
    {
        checkNodeList.Clear();
        nodesTag = new StringBuilder();
        TreeNode node = TreeView.Nodes[0];//得到treeview的根结点
        if (node.Text == "请选择")
        {
            while (node.PrevNode != null)
            {
                node = node.PrevNode;
            }
        }
        TraversNodes(node);//遍历根结点
        return nodesName.ToString();
    }

    public void treeView_AfterSelect(object sender, TreeViewEventArgs e)
    {
        //this.Text = TreeView.SelectedNode.Text.ToString();
        //TreeView.SelectedNode.Checked = true;
        ////TreeView_AfterCheck(sender, e);
        //dropDown.Close();
    }
    public TreeView TreeView
    {
        get { return treeViewHost.Control as TreeView; }
    }
    private void ShowDropDown()
    {
        if (dropDown != null)
        {
            //DropDownHeight
            treeViewHost.Size = new System.Drawing.Size(DropDownWidth - 2, 300);
            dropDown.Show(this, 0, this.Height);
        }
    }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
        {
            ShowDropDown();
            return;
        }
        base.WndProc(ref m);
    }

    //private void InitializeComponent()
    //{
    //    this.SuspendLayout();
    //    this.ResumeLayout(false);

    //}

    //protected override void Dispose(bool disposing)
    //{
    //    if (disposing)
    //    {
    //        if (dropDown != null)
    //        {
    //            dropDown.Dispose();
    //            dropDown = null;
    //        }
    //    }
    //    base.Dispose(disposing);
    //}

    /// <summary>
    /// 遍历方法
    /// </summary>
    /// <param name="parent"></param>
    private void TraversNodes(TreeNode parent)
    {
        TreeNode node = parent;
        if (node != null)
        {
            //if (node.Tag != null && node.Tag.ToString() == tag)
            //{
            if (node.Text != "请选择")
            {
                if (node.Text != null && !string.IsNullOrEmpty(node.Text.ToString()) && node.Checked == true)
                {
                    if (!nodesName.ToString().Contains(node.Text))
                    {
                        if (!checkNodeList.Contains(node.Tag.ToString()))
                        {
                            checkNodeList.Add(node.Tag.ToString());
                        }                       
                        //string ss = node.Text.Replace(')', ' ');
                        //nodesTag.AppendFormat("{0},", ss.Substring(ss.LastIndexOf('(') + 1));
                        //string ss1 = node.Text.Replace('(', ' ');

                        nodesName.AppendFormat("{0},", node.Text.ToString());
                    }
                    //else
                    //{
                    //    string ss = node.Text.Replace(')', ' ');
                    //    nodesTag.AppendFormat("{0},", ss.Substring(ss.LastIndexOf('(') + 1));
                    //}
                }
                else if (node.Text != null && !string.IsNullOrEmpty(node.Text.ToString()) && node.Checked == false)
                {
                    if (nodesName.ToString().Contains(node.Text))
                    {
                        //string ss = node.Text.Replace(')', ' ');
                        //nodesTag.AppendFormat("{0},", ss.Substring(ss.LastIndexOf('(') + 1));
                        //string ss1 = node.Text.Replace('(', ' ');
                        nodesName.Replace(node.Text + ",", "");
                        //nodesName.AppendFormat("{0},", node.Text.ToString());
                    }
                    //else
                    //{
                    //    string ss = node.Text.Replace(')', ' ');
                    //    nodesTag.AppendFormat("{0},", ss.Substring(ss.LastIndexOf('(') + 1));
                    //}
                }
            }

            if (node.FirstNode != null)//如果node节点还有子节点则进入遍历
                {
                    TraversNodes(node.FirstNode);
                }
                if (node.NextNode != null)//如果node节点后面有同级节点则进入遍历
                {
                    TraversNodes(node.NextNode);
                }
           
        }
    }


}
